package com.API;

import com.Properties.MerData;
import com.Properties.Url;
import com.Utils.HttpClientUtil;
import com.Utils.Tools.SignEncrypt;
import org.apache.http.message.BasicNameValuePair;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.Utils.Tools.FileTwoMd5.calculateMD5OfFile;
import static com.Utils.Tools.XMLResponseParser.parseXML;

public class PayTransferContractFileUpload {

    // 上传协议文件接口
    public static void main(String[] args) {
        String fileContent = "D:\\picture\\121212.pdf";
        String fileSign;
        String version = "3";
        String agentId;
        String idCardNo = "100000200001010123";
        String key;
        String signStr;
        String sign;
        String result;
        MerData merData = new MerData();
        String ret_code;
        String ret_msg;
        String file_id;
        String ResponseSign;

        try {
            // 读取本地文件并且调用calculateMD5OfFile方法计算文件的MD5值file_sign
            fileSign = calculateMD5OfFile(fileContent);

            // 定义参数，包括：version，agent_id，id_card_no，file_sign
            agentId = merData.getAgentId();
            key = merData.getMd5Key();

            // 计算sign的值
            signStr = ("agent_id=" + agentId + "&file_sign=" + fileSign + "&id_card_no=" + idCardNo + "&key=" + key + "&version=" + version).toLowerCase();
            System.out.println("待签名串------------------------->" + signStr);
            sign = SignEncrypt.getMD5utf8(signStr);
            System.out.println("签名结果------------------------->" + sign);

            // 添加参数
            List<BasicNameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("agent_id", agentId));
            params.add(new BasicNameValuePair("id_card_no", idCardNo));
            params.add(new BasicNameValuePair("file_sign", fileSign));
            params.add(new BasicNameValuePair("version", version));
            params.add(new BasicNameValuePair("sign", sign));
            //打印请求参数
            System.out.println("请求参数---------->" + params);

            // 调用接口，使用MultipartFormData上传文件，并返回结果
            String payTransferContractFileUploadUrl = Url.PayTransferContractFileUpload;
            result = HttpClientUtil.MultipartFormData(payTransferContractFileUploadUrl, params, fileContent);
            System.out.println("上传协议文件接口返回结果---------->" + result);
            //解析响应结果 <?xml version="1.0" encoding="utf-8"?><root><ret_code>0000</ret_code><ret_msg>文件上传成功</ret_msg><agent_id>1664502</agent_id><file_id>17369335780440af485cc16645026064</file_id><sign>0eca786c8e2c770d3ebf7c1a9170288b</sign></root> 并拼接验签串
            Map<String, String> elements = parseXML(result);
            ret_code = elements.get("ret_code");
            ret_msg = elements.get("ret_msg");
            file_id = elements.get("file_id");
            ResponseSign = elements.get("sign");
            if ("0000".equals(ret_code)){
                System.out.println("文件上传成功，file_id为---------->" + file_id);
                //验签
                String checkSignStr = ("agent_id=" + agentId + "&file_id=" + file_id  + "&key=" + key + "&ret_code=" + ret_code + "&ret_msg=" + ret_msg).toLowerCase();
                String checkSign = SignEncrypt.getMD5utf8(checkSignStr);
                if (Objects.equals(checkSign, ResponseSign)){
                    System.out.println("同步验签返回结果---------->验签成功");
                }else {
                    System.out.println("同步验签返回结果---------->验签失败");
                }
            }else {
                System.out.println("文件上传失败，原因---------->" + ret_msg);
            }

        } catch (NoSuchAlgorithmException e) {
            System.err.println("签名算法错误: " + e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("文件读取或上传错误: " + e.getMessage());
            e.printStackTrace();
        } catch (ParserConfigurationException | SAXException e) {
            throw new RuntimeException(e);
        }
    }
}
